package com.htt.app.blogradio.notifi;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;

import com.htt.app.blogradio.BlogRadio;
import com.htt.app.blogradio.R;

public class NotificationHelper {
    private Context mContext;
    private int NOTIFICATION_ID = 1;
    private Notification mNotification;
    private NotificationManager mNotificationManager;
    private PendingIntent mContentIntent;
    private String mContentTitle;
    private String mContentText;
    CharSequence tickerText;
    int icon;

    public NotificationHelper(Context context, String title, String text) {
        mContext = context;
        this.mContentTitle = title;
        this.mContentText = text;
    }

    @SuppressWarnings("deprecation")
	public void createNotification(int newtask) {

        mNotificationManager = (NotificationManager) mContext
                .getSystemService(Context.NOTIFICATION_SERVICE);
        NOTIFICATION_ID = newtask;
        icon = R.drawable.ic_launcher;
        tickerText = mContext.getString(R.string.app_name);
        long when = System.currentTimeMillis();
        mNotification = new Notification(icon, tickerText, when);
        Intent notificationIntent = new Intent(mContext, BlogRadio.class);
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
        mContentIntent = PendingIntent.getActivity(mContext, 0,
                notificationIntent, 0);
        mNotification.setLatestEventInfo(mContext, mContentTitle, mContentText,
                mContentIntent);
        mNotification.flags = Notification.FLAG_AUTO_CANCEL;
        mNotificationManager.notify(NOTIFICATION_ID, mNotification);
    }

    @SuppressWarnings("deprecation")
	public void update(String title, String text) {
    	mContentTitle = title;
    	mContentText = text;
        mNotification.setLatestEventInfo(mContext, mContentTitle, mContentText,
                mContentIntent);
        mNotificationManager.notify(NOTIFICATION_ID, mNotification);
    }

    public void completed() {

        mNotificationManager.cancel(NOTIFICATION_ID);
        Notification completeNotification = new Notification(icon, tickerText,
                System.currentTimeMillis());

        try {
            PackageManager packageManager = mContext.getPackageManager();
            Intent completeIntent = new Intent();


            PendingIntent cIntent = PendingIntent.getActivity(mContext, 0,
                    completeIntent, 0);
            completeNotification.setLatestEventInfo(mContext, mContentTitle,
                    "Download Complete", cIntent);
            completeNotification.flags = Notification.FLAG_AUTO_CANCEL;
            mNotificationManager.notify(NOTIFICATION_ID, completeNotification);
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }

    }

    public void clearNotification() {
        mNotificationManager.cancel(NOTIFICATION_ID);
    }

}